﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WISD.CarFriend.Model;
using WISD.Common;
using System.Data;
using System.Data.Common;
using System.Data.Objects;

namespace WISD.CarFriend.BLL.Web
{
    public static class InformationManageModule
    {
        /// <summary>
        /// 新增资讯信息
        /// </summary>
        /// <param name="info"></param>
        public static void InsertNewInfo(T_CF_Web_Information info)
        {
            using (CarFriendDBContext db = new CarFriendDBContext())
            {
                info.State = 1;
                info.CreateTime = DateTime.Now;
                db.T_CF_Web_Information.AddObject(info);
                db.SaveChanges();
            }
        }


        /// <summary>
        /// 根据主键获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T_CF_Web_Information GetInfoByID(int id)
        {
            T_CF_Web_Information info = new T_CF_Web_Information();
            using (CarFriendDBContext db = new CarFriendDBContext())
            {
                info = db.T_CF_Web_Information.FirstOrDefault(p => p.InformationID == id && p.State == 1);
            }
            return info;
        }


        /// <summary>
        /// 更新文档的浏览数
        /// </summary>
        /// <param name="id"></param>
        public static void UpdateHits(int id)
        {
            using (CarFriendDBContext db = new CarFriendDBContext())
            {
                T_CF_Web_InfoHit newInfo = db.T_CF_Web_InfoHit.FirstOrDefault(p => p.InfoID==id);
                if (newInfo != null)
                {
                    newInfo.Hits = newInfo.Hits ?? 0;
                    newInfo.Hits += 1;
                }
                else
                {
                    newInfo = new T_CF_Web_InfoHit();
                    newInfo.InfoID = id;
                    newInfo.Hits = 1;
                    db.T_CF_Web_InfoHit.AddObject(newInfo);
                }
                db.SaveChanges();
            }
        }


        /// <summary>
        /// 更新资讯
        /// </summary>
        /// <param name="MyInfo"></param>
        public static void UpdateNewInfo(T_CF_Web_Information MyInfo)
        {
            using (CarFriendDBContext db = new CarFriendDBContext())
            {
                T_CF_Web_Information newInfo = db.T_CF_Web_Information.FirstOrDefault(p => p.InformationID == MyInfo.InformationID);
                if (newInfo != null)
                {
                    newInfo.Title = MyInfo.Title;
                    newInfo.TypeID = MyInfo.TypeID;
                    newInfo.IsTop = MyInfo.IsTop;
                    newInfo.InfoContent = MyInfo.InfoContent;
                    newInfo.UpdaterID = MyInfo.UpdaterID;
                    newInfo.UpdateTime = DateTime.Now;
                    db.SaveChanges();
                }
            }
        }


//        public static DataSet GetInfoList()
//        {
//            using (CarFriendDBContext db = new CarFriendDBContext())
//            {
//                string sql = string.Format(@"  select t.InformationID,t.Title,ty.TypeName, convert(varchar(12),t.CreateTime, 23)as CreateDate,  isnull(hit.Hits,0)as Hits,COUNT(com.InfoCommentID) as ComCount
//                                                  from dbo.T_CF_Web_Information as t
//                                                  left join dbo.T_CF_Web_InfoHit as hit
//                                                  on t.InformationID=hit.InfoID
//                                                  left join dbo.T_CF_Web_InfoCommnet as com
//                                                  on com.InfoID= t.InformationID
//                                                  left JOIN dbo.T_CF_Web_Type as ty
//                                                  on ty.TypeID= t.TypeID
//                                                  where t.State=1  
//                                                  group by t.InformationID,t.Title,hit.Hits,ty.TypeName,t.CreateTime");
//               ObjectQuery<MyInfo> inf = db.CreateQuery<MyInfo>(sql);
//                return EFHelper.ListToDataSet<MyInfo>(inf.Execute(MergeOption.AppendOnly).ToList<MyInfo>());
//            }
//        }


        /// <summary>
        /// 生成 资讯列表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetInfoList()
        {
            DataTable newdtb = new DataTable("Information");
            newdtb.Columns.Add("InformationID", typeof(int));
            newdtb.Columns.Add("Title", typeof(string));
            newdtb.Columns.Add("TypeName", typeof(string));
            newdtb.Columns.Add("CreateDate", typeof(string));
            newdtb.Columns.Add("Hits", typeof(decimal));
            newdtb.Columns.Add("ComCount", typeof(decimal));
            List<T_CF_Web_Information> infolist = new List<T_CF_Web_Information>();
            using (CarFriendDBContext db = new CarFriendDBContext())
            {
                infolist = db.T_CF_Web_Information.Where(p => p.State == 1).OrderByDescending(p => p.CreateTime).ToList();
            }
            infolist.ForEach(p =>
            {
                DataRow newRow = newdtb.NewRow();
                newRow["InformationID"] = p.InformationID;
                newRow["Title"] = p.Title;
                newRow["CreateDate"] = p.CreateTime.Value.ToString();
                string typename = "";
                using (CarFriendDBContext db = new CarFriendDBContext())
                {
                    var type = db.T_CF_Web_Type.FirstOrDefault(q => q.TypeID == p.TypeID);
                    typename = type == null ? "" : type.TypeName;
                }
                newRow["TypeName"] = typename;
                decimal hits = 0;
                T_CF_Web_InfoHit infohite = new T_CF_Web_InfoHit();
                using (CarFriendDBContext db = new CarFriendDBContext())
                {
                    infohite = db.T_CF_Web_InfoHit.FirstOrDefault(q => q.InfoID == p.InformationID);
                    hits = infohite == null ? 0 : (infohite.Hits ?? 0);
                }
                newRow["Hits"] = hits;
                decimal count = 0;
                using (CarFriendDBContext db = new CarFriendDBContext())
                {
                    count = db.T_CF_Web_InfoCommnet.Count(q => q.InfoID == p.InformationID && q.State == 1);
                }
                newRow["ComCount"] = count;
                newdtb.Rows.Add(newRow);
            });
            return newdtb;
        }


        /// <summary>
        /// 生成 资讯列表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetInfoList(int typeID)
        {
            DataTable newdtb = new DataTable("Information");
            newdtb.Columns.Add("InformationID", typeof(int));
            newdtb.Columns.Add("Title", typeof(string));
            
            List<T_CF_Web_Information> infolist = new List<T_CF_Web_Information>();
            using (CarFriendDBContext db = new CarFriendDBContext())
            {
                infolist = db.T_CF_Web_Information.Where(p => p.State == 1
                    && p.TypeID==typeID).OrderByDescending(p=>p.IsTop)
                    .ThenByDescending(p => p.CreateTime).ToList();
            }
            infolist.ForEach(p =>
            {
                DataRow newRow = newdtb.NewRow();
                newRow["InformationID"] = p.InformationID;
                newRow["Title"] = p.Title.Length > 30 ? p.Title.Substring(0, 30) + "..." : p.Title;
                newdtb.Rows.Add(newRow);
            });

            return newdtb;
        }


        /// <summary>
        /// 逻辑删除某个资讯
        /// </summary>
        /// <param name="id"></param>
        /// <param name="loginID"></param>
        public static void DelectInfo(int id,int loginID)
        {
            using (CarFriendDBContext db = new CarFriendDBContext())
            {
                T_CF_Web_Information newInfo = db.T_CF_Web_Information.FirstOrDefault(p => p.InformationID == id);
                if (newInfo != null)
                {
                    newInfo.UpdateTime = DateTime.Now;
                    newInfo.UpdaterID = loginID;
                    newInfo.State=0;
                    db.SaveChanges();
                }
            }
        }


        /// <summary>
        /// 逻辑删除某个资讯
        /// </summary>
        /// <param name="id"></param>
        /// <param name="loginID"></param>
        public static void DelectInfoComm(int id, int loginID)
        {
            using (CarFriendDBContext db = new CarFriendDBContext())
            {
                T_CF_Web_InfoCommnet newInfoComm = db.T_CF_Web_InfoCommnet.FirstOrDefault(p => p.InfoCommentID ==id);
                if (newInfoComm != null)
                {
                    newInfoComm.UpdateTime = DateTime.Now;
                    newInfoComm.UpdaterID = loginID;
                    newInfoComm.State = 0;
                    db.SaveChanges();
                }
            }
        }


        /// <summary>
        /// 根据资讯ID获取所有评论信息
        /// </summary>
        /// <param name="infoID"></param>
        /// <returns></returns>
        public static DataTable GetInfoComList(int infoID)
        {
            DataTable newdtb = new DataTable("InfoComment");
            newdtb.Columns.Add("InfoCommentID", typeof(int));
            newdtb.Columns.Add("SingeContent", typeof(string));
            newdtb.Columns.Add("CommContent", typeof(string));
            newdtb.Columns.Add("CreateData", typeof(string));
            List<T_CF_Web_InfoCommnet> infocomlist = new List<T_CF_Web_InfoCommnet>();
            using (CarFriendDBContext db = new CarFriendDBContext())
            {
                infocomlist = db.T_CF_Web_InfoCommnet.Where(p => p.State == 1
                    &&p.InfoID==infoID).OrderByDescending(p => p.CreateTime).ToList();
            }
            infocomlist.ForEach(p =>
            {
                DataRow newRow = newdtb.NewRow();
                newRow["InfoCommentID"] = p.InfoCommentID;
                newRow["SingeContent"] = p.CommContent.Length > 30 ? p.CommContent.Substring(0, 30) + "..." : p.CommContent;
                newRow["CommContent"] = p.CommContent;
                newRow["CreateData"] = p.CreateTime.HasValue ? (p.CreateTime.Value.ToString("yyyy-MM-dd")) : "";
                newdtb.Rows.Add(newRow);
            });

            return newdtb;
        }


        /// <summary>
        /// 新增评论
        /// </summary>
        /// <param name="info"></param>
        public static void InsertNewInfoComm(T_CF_Web_InfoCommnet info)
        {
            using (CarFriendDBContext db = new CarFriendDBContext())
            {
                info.State = 1;
                info.CreateTime = DateTime.Now;
                db.T_CF_Web_InfoCommnet.AddObject(info);
                db.SaveChanges();
            }
        }


        /// <summary>
        /// 获取资讯InfoXML
        /// </summary>
        /// <param name="top">显示条数</param>
        /// <param name="txtNum">显示字数</param>
        /// <param name="typeID">消息类型</param>
        /// <returns></returns>
        public static string GetInfoXML(int top, int num, int typeID)
        {
            StringBuilder xml = new StringBuilder();

            DataTable InfoTable = new DataTable();
            using (CarFriendDBContext db = new CarFriendDBContext())
            {
                List<T_CF_Web_Information> infolist = db.T_CF_Web_Information.Where(p => p.State == 1
                    && p.TypeID == typeID).OrderByDescending(p => p.IsTop)
                    .ThenByDescending(p => p.CreateTime).Take(8).ToList();
                InfoTable = EFHelper.ListToDataTable<T_CF_Web_Information>(infolist);
            }

            if (InfoTable.Rows.Count > 0)
            {
                for (int i = 0; i < InfoTable.Rows.Count; i++)
                {
                    DataRow dr = InfoTable.Rows[i];
                    xml.Append("<li>");
                    xml.Append("<a target=\"_blank\" title=\"" + dr["Title"].ToString() + "\" href=\"/Module/Information/InfoDetail.aspx?id=" + dr["InformationID"] + "\">" + StringHandle.CutString(dr["Title"].ToString(), num) + "</a>");
                    xml.Append("</li>");
                }
            }
            else
            {
                xml.Append("<li>暂无数据显示...</li>");
            }

            return xml.ToString();
        }


    }
}
